package com.getsendsocial.GetSendSocial.Activity;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.getsendsocial.GetSendSocial.R;
import com.getsendsocial.GetSendSocial.Database.DbActivity;
import com.getsendsocial.GetSendSocial.Util.AddTab;
import com.getsendsocial.GetSendSocial.Util.GetTab;
import com.getsendsocial.GetSendSocial.Util.StartTab;



public class ABSTabsViewPagerActivity extends SherlockFragmentActivity{

	ViewPager mViewPager;
	TabsAdapter mTabsAdapter;
	TextView tabCenter;
	TextView tabText;
	public static Context c;
	public static ProgressDialog dialog;
	static ConnectivityManager conMan;


	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		
		c = ABSTabsViewPagerActivity.this;
		super.onCreate(savedInstanceState);
		
		//conMan= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		mViewPager = new ViewPager(this);
		mViewPager.setId(R.id.pager);

		setContentView(mViewPager);
		ActionBar bar = getSupportActionBar();
		bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		mTabsAdapter = new TabsAdapter(this, mViewPager);
		
		Tab tab1 = bar.newTab();
		RelativeLayout view1 = (RelativeLayout) getLayoutInflater().inflate(R.layout.label1, null);
		tab1.setCustomView(view1);
		

		mTabsAdapter.addTab(tab1,StartTab.class, null);
		
		Tab tab2 = bar.newTab();
		RelativeLayout view2 = (RelativeLayout) getLayoutInflater().inflate(R.layout.label2, null);
		tab2.setCustomView(view2);
		
		mTabsAdapter.addTab(tab2,GetTab.class, null);
		
		Tab tab3 = bar.newTab();
		RelativeLayout view3 = (RelativeLayout) getLayoutInflater().inflate(R.layout.label3, null);
		tab3.setCustomView(view3);
		
		mTabsAdapter.addTab(tab3,AddTab.class, null);
		
		  
	}
	

	public static class TabsAdapter extends FragmentPagerAdapter implements
			ActionBar.TabListener, ViewPager.OnPageChangeListener
	{
		private final Context mContext;
		private final ActionBar mActionBar;
		private final ViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

		static final class TabInfo
		{
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(Class<?> _class, Bundle _args)
			{
				clss = _class;
				args = _args;
			}
		}

		public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager)
		{
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mActionBar = activity.getSupportActionBar();
			mViewPager = pager;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
		{
			TabInfo info = new TabInfo(clss, args);
			tab.setTag(info);
			tab.setTabListener(this);
			mTabs.add(info);
			mActionBar.addTab(tab);
			notifyDataSetChanged();
		}

		@Override
		public int getCount()
		{
			return mTabs.size();
		}

		@Override
		public Fragment getItem(int position)
		{
			TabInfo info = mTabs.get(position);
			return Fragment.instantiate(mContext, info.clss.getName(),
					info.args);
		}

		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels)
		{
		}

		public void onPageSelected(int position)
		{
			mActionBar.setSelectedNavigationItem(position);
		}

		public void onPageScrollStateChanged(int state)
		{
		}

		public void onTabSelected(Tab tab, FragmentTransaction ft)
		{
			Object tag = tab.getTag();
			for (int i = 0; i < mTabs.size(); i++)
			{
				if (mTabs.get(i) == tag)
				{
					mViewPager.setCurrentItem(i);
				}
			}
		}

		public void onTabUnselected(Tab tab, FragmentTransaction ft)
		{
		}

		public void onTabReselected(Tab tab, FragmentTransaction ft)
		{
		}
		
	}
	
	@Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
	
	 @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        super.onOptionsItemSelected(item);
	        
	        
	 
	        if(item.getItemId()==R.id.sync){
	            	if (checkInternetConnection()==true){
	            		
	            		
	            		dialog = new ProgressDialog(ABSTabsViewPagerActivity.this);
	            		dialog.setMessage("Aggiorno il Database..");
	            		dialog.show();
	            	
	            		new DbActivity(ABSTabsViewPagerActivity.this).execute();
	            	}
	        }
	        
	        if(item.getItemId()==R.id.Info){
	            	Intent intent = new Intent(ABSTabsViewPagerActivity.this,InfoActivity.class);
	            	startActivity(intent);
	        }
	        return true;
	 }

	 private boolean checkInternetConnection() {
			ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
			State mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
			State wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
			
			
			if (mobile == NetworkInfo.State.DISCONNECTED && wifi == NetworkInfo.State.DISCONNECTED){
				Toast.makeText(c, "Devi abilitare una connessione di rete per aggiornare il tuo database!",Toast.LENGTH_LONG).show();
				Handler handler = new Handler();
				handler.postDelayed(new Runnable() {
					
					public void run() {
						startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));

					}
				}, 800);
				return false;
			}
			return true; 
		}

		
		
	}
